package net.expectx.micro.shop.controller.content;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sun.tools.javac.util.Convert;
import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseResult;
import net.expectx.micro.shop.common.AdminConstants;
import net.expectx.micro.shop.common.util.ExpectXUtil;
import net.expectx.micro.shop.dao.bean.*;
import net.expectx.micro.shop.rpc.api.ContentAdvertisingActivitiesService;
import net.expectx.micro.shop.rpc.api.ProductProductService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.util.*;

/**
 * @author hxs
 */
@Controller
@RequestMapping(value = "/manager/content/activities")
public class ContentActivitiesController {
    @Autowired
    ContentAdvertisingActivitiesService contentAdvertisingActivitiesService;
    @Autowired
    ProductProductService productProductService;

    private final  static String ADVERTISING_TYPE="2";

    @ApiOperation(value = "产品活动主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index( HttpServletRequest request) {
        return "content/activities/index";
    }

    @ApiOperation(value = "新增产品活动")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "content/activities/modify";
    }

    @ApiOperation(value = "修改产品活动")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        ContentAdvertisingActivities contentActivities = contentAdvertisingActivitiesService.selectByPrimaryKey(id);
        modelMap.put("contentActivities", contentActivities);
        return "content/activities/modify";
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request,String productName) {
        Map<String, Object> result = new HashMap<>(16);
        ContentAdvertisingActivitiesExample contentAdvertisingActivitiesExample = new ContentAdvertisingActivitiesExample();

        /**
         * 创造条件
         */
        ContentAdvertisingActivitiesExample.Criteria criteria = contentAdvertisingActivitiesExample.createCriteria();

        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        criteria.andTypeEqualTo(ADVERTISING_TYPE);
        if (StringUtils.isNotBlank(productName)){
            criteria.andProductNameLike("%"+productName+"%");
        }
        /**
         * 分页
         */
        contentAdvertisingActivitiesExample.setPageIndex(page);
        contentAdvertisingActivitiesExample.setPageSize(limit);
        /**
         * 排序
         */
        contentAdvertisingActivitiesExample.setOrderByClause("gmt_modified desc");

        List<ContentAdvertisingActivities> contentAdvertisingActivities = contentAdvertisingActivitiesService.selectByExample(contentAdvertisingActivitiesExample);

        long total = contentAdvertisingActivitiesService.countByExample(contentAdvertisingActivitiesExample);

        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", contentAdvertisingActivities);
        return result;
    }

    @ApiOperation(value = "新增产品活动")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(ContentAdvertisingActivities contentAdvertisingActivities) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        PermissionUser permissionUser =(PermissionUser) session.getAttribute("permissionUser");
        ProductProduct productProduct = productProductService.selectByPrimaryKey(contentAdvertisingActivities.getProductId());
        if (null!=productProduct){
            contentAdvertisingActivities.setGmtCreate(System.currentTimeMillis());
            contentAdvertisingActivities.setGmtModified(System.currentTimeMillis());
            contentAdvertisingActivities.setIsDeleted((byte) 0);
            contentAdvertisingActivities.setType(ADVERTISING_TYPE);
            contentAdvertisingActivities.setUserId(permissionUser.getId());
            contentAdvertisingActivities.setProductName(productProduct.getProductName());
            contentAdvertisingActivities.setImages(productProduct.getProductImg());
            return contentAdvertisingActivitiesService.insertSelective(contentAdvertisingActivities);
        }
        return new BaseResult(false,"新增失败",null);
    }

    @ApiOperation(value = "修改产品活动")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, ContentAdvertisingActivities contentAdvertisingActivities) {
        ProductProduct productProduct = productProductService.selectByPrimaryKey(contentAdvertisingActivities.getProductId());
        if (null!=productProduct){
            contentAdvertisingActivities.setProductName(productProduct.getProductName());
            contentAdvertisingActivities.setGmtModified(System.currentTimeMillis());
            return  contentAdvertisingActivitiesService.updateByPrimaryKeySelective(contentAdvertisingActivities);
        }
        return new BaseResult(false,"修改失败",null);
    }

    @ApiOperation(value = "删除产品活动")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object physicalRemove(@PathVariable("ids") String ids) {
        ContentAdvertisingActivitiesExample contentAdvertisingActivitiesExample = new ContentAdvertisingActivitiesExample();
        contentAdvertisingActivitiesExample.createCriteria().andIdIn(ExpectXUtil.toList(ids,","));
        return contentAdvertisingActivitiesService.physicalRemoveByExample(AdminConstants.YES, contentAdvertisingActivitiesExample);
    }

    /**
     * 添加产品活动 miniui lookup获取数据
     * @param pageIndex
     * @param pageSize
     * @param key
     * @return
     */
    @RequestMapping(value = "/projectList", method = RequestMethod.POST)
    @ResponseBody
    public Object getProjectList(Integer pageIndex,Integer pageSize,String key){
        Map<String, Object> result = new HashMap<>(16);
        ProductProductExample productProductExample = new ProductProductExample();
        ProductProductExample.Criteria criteria = productProductExample.createCriteria();
        productProductExample.setPageIndex(pageIndex+1);
        productProductExample.setPageSize(pageSize);
        if (StringUtils.isNotBlank(key)){
            productProductExample.or().andProductSnLike("%"+key+"%");
            productProductExample.or().andProductNameLike("%"+key+"%");
        }
        List<ProductProduct> productProducts = productProductService.selectByExample(productProductExample);
        result.put("data",productProducts);
        result.put("total",productProductService.countByExample(productProductExample));
        return JSON.toJSONString(result);
    }
}
